/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.controller.alioss;

import com.shop.cereshop.app.alioss.page.Url;
import com.shop.cereshop.app.alioss.service.FileUploadService;
import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.upload.strategy.FileStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传
 */
@RestController
@RequestMapping("file")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "UploadController")
@Api(value = "文件上传", tags = "文件上传")
public class UploadController {

    /*@Autowired
    private FileUploadService fileUploadService;*/

    @Autowired
    private FileStrategy fileStrategy;


    @Autowired
    CereBuyerUserService cereBuyerUserService;

    /**
     * 文件上传
     * @param file
     */
    @PostMapping("upload")
    @ApiOperation(value = "文件上传")
    public Result<Url> upload(MultipartFile file) throws Exception{
        Result result=new Result();
        try {
            log.info("upload-------------------------------");
            if(null != file){

                String url=fileStrategy.upload(file);
//                fileUploadService.uploadFile(file.getOriginalFilename(),file.getInputStream(),file.getSize());

                result=new Result(new Url(url),CoReturnFormat.SUCCESS);

            }

            return result;
        } catch (Exception e) {
            log.error("upload fail " + e.getMessage(), e);
        }
        return result;
    }



    @RequestMapping(value = "uploadUserImages", method = {RequestMethod.PUT, RequestMethod.POST})
    @NoRepeatSubmit
    @ApiOperation(value = "上传用户图片")
    @NoRepeatWebLog
    public Result<Object> uploadUserImages(
            @RequestParam(required = false) MultipartFile headImageFile,
            @RequestParam(required = false) MultipartFile backgroundFile,
            HttpServletRequest request) throws CoBusinessException {

        // 获取当前登录账户
        CereBuyerUser cereBuyerUser = (CereBuyerUser) request.getAttribute("user");
        cereBuyerUser = cereBuyerUserService.selectByBuyerUserId(cereBuyerUser.getBuyerUserId());

        Long buyerUserId = null;

        // 处理头像上传
        if (headImageFile != null && !headImageFile.isEmpty()) {
            buyerUserId = cereBuyerUser.getBuyerUserId();
            try {
                String headImageUrl = fileStrategy.upload(headImageFile);
                cereBuyerUser.setHeadImage(headImageUrl);
            } catch (Exception e) {
                log.error("上传头像失败", e);
                return new Result<>(CoReturnFormat.UPLOAD_FAIL);
            }
        }

        // 处理背景图上传
        if (backgroundFile != null && !backgroundFile.isEmpty()) {
            try {
                String backgroundUrl = fileStrategy.upload(backgroundFile);
                cereBuyerUser.setBackgroundImage(backgroundUrl);
            } catch (Exception e) {
                log.error("上传背景图失败", e);
                return new Result<>(CoReturnFormat.UPLOAD_FAIL);
            }
        }
//        根据用户id修改用户的背景图和头像
        cereBuyerUserService.updaBgAndHeadImage(cereBuyerUser);
        return new Result<>(CoReturnFormat.SUCCESS);
    }





}
